package com.caul.demo.hadoop.mapreduce.ext;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.output.MultipleOutputs;

import java.io.IOException;

/**
 * Created by sdliang on 2018/4/13.
 */
public class CustomMapper extends Mapper<LongWritable, Text, Text, CustomWritable> {

  /**
   * 多路输出器
   */
  MultipleOutputs<Text, CustomWritable> outputs;

  @Override
  protected void setup(Context context) throws IOException, InterruptedException {
    outputs = new MultipleOutputs<>(context);
  }

  @Override
  protected void map(LongWritable key, Text value, Context context)
      throws IOException, InterruptedException {
    String str = value.toString();
    if (str.startsWith("a")) {//以a开头的内容输出到a/文件夹下
      outputs.write(value, new CustomWritable(), "a/");
    } else if (str.startsWith("b")) {
      outputs.write(value, new CustomWritable(), "b/");
    } else {
      outputs.write(value, new CustomWritable(), "other/");
    }
  }

  @Override
  protected void cleanup(Context context) throws IOException, InterruptedException {
    outputs.close();
  }
}
